package chess;

import java.awt.Color;
import java.util.HashSet;
import java.util.Set;

public class Pawn extends Piece{

	private String name;
	private String type;
	private Color color;
	private Coordinate co;
	private boolean moved= false;
	private boolean selected;
	private Coordinate lastCo;
	public static boolean enPassant= false;

	public Pawn(String name, String type, Color color, Coordinate co){

		this.name= name;
		this.type= type;
		this.color= color;
		this.co= co;
	}

	public Set<Coordinate> getPossibleCoordinates(){
		int coX= co.getX();
		int coY= co.getY();
		Set<Coordinate> posCoords= new HashSet<Coordinate>();

		Coordinate c1;
		Coordinate c2;
		Coordinate c3;
		Coordinate c4;

		if (color.equals(Color.BLACK)){
			c1= new Coordinate(coX+1, coY+1);
			c2= new Coordinate(coX-1, coY+1);
			c3= new Coordinate(coX, coY+1);
			c4= new Coordinate(coX, coY+2);
		}
		else {
			c1= new Coordinate(coX-1, coY-1);
			c2= new Coordinate(coX+1, coY-1);
			c3= new Coordinate(coX, coY-1);
			c4= new Coordinate(coX, coY-2);
		}
		//Checks En Passant
		Piece op1= GameRunner.board.getPiece(c1.getX(), coY);
		if (op1!= null && op1.getType()== "Pawn")
			op1= (Pawn) op1;
		if (GameRunner.board.isValid(c1)){
			if (GameRunner.board.isOccupied(c1) && 
				GameRunner.board.getPiece(c1).getColor()!= color)
					posCoords.add(c1);
			else if (!(GameRunner.board.isOccupied(c1)) &&
					GameRunner.board.isOccupied(new Coordinate(c1.getX(), coY)) &&
					op1.getColor()!= color ){
					if (color== Color.BLACK && coY== 4){
						if (GameRunner.lastMove.p.equals(op1) &&
							Math.abs(GameRunner.lastMove.p.getCoordinate().getY()-GameRunner.lastMove.oldCoords.getY())==2){
								posCoords.add(c1);
								enPassant= true;
						}
					}
					else if (color== Color.WHITE && coY== 3){
						if (GameRunner.lastMove.p.equals(op1) &&
							Math.abs(GameRunner.lastMove.p.getCoordinate().getY()-GameRunner.lastMove.oldCoords.getY())==2){
								posCoords.add(c1);
								enPassant= true;
						}
					}
			}
		}
		//Checks En Passant
		Piece op2= GameRunner.board.getPiece(c2.getX(), coY);
		if (op2!= null && op2.getType()== "Pawn")
			op2= (Pawn) op2;
		if (GameRunner.board.isValid(c2)){
			if (GameRunner.board.isOccupied(c2) && 
					GameRunner.board.getPiece(c2).getColor()!= color)
				posCoords.add(c2);
			else if (!(GameRunner.board.isOccupied(c2)) &&
					GameRunner.board.isOccupied(new Coordinate(c2.getX(), coY)) &&
					op2!= null && op2.getColor()!= color){
					if (color== Color.BLACK && coY== 4){
						if (GameRunner.lastMove.p.equals(op2) &&
							Math.abs(GameRunner.lastMove.p.getCoordinate().getY()-GameRunner.lastMove.oldCoords.getY())==2){
								posCoords.add(c2);
								enPassant= true;
						}
					}
					else if (color== Color.WHITE && coY== 3){
						if (GameRunner.lastMove.p.equals(op2) &&
							Math.abs(GameRunner.lastMove.p.getCoordinate().getY()-GameRunner.lastMove.oldCoords.getY())==2){
								posCoords.add(c2);
								enPassant= true;
						}
					}
			}
		}	
		if (GameRunner.board.isValid(c3) &&
			!(GameRunner.board.isOccupied(c3)))
				posCoords.add(c3);
		if (GameRunner.board.isValid(c4) &&
			!(GameRunner.board.isOccupied(c4)) &&
			!(GameRunner.board.isOccupied(c3)) &&
			!moved)
				posCoords.add(c4);
		return posCoords;
	}

	public void moveTo(Coordinate c){
		this.setCoordinate(c);
		moved= true;
	}

	public Coordinate getCoordinate(){
		return co;
	}

	public String getType(){
		return type;
	}
	
	public void setCoordinate(Coordinate c, Grid g){
		co= c;
		g.setPieceToCoordinate(c, this);
	}

	public void setCoordinate(Coordinate c){
		lastCo= co;
		co= c;
		GameRunner.board.setPieceToCoordinate(c,this);
	}

	public String getName(){
		return name;
	}

	public Color getColor(){
		return color;
	}
	
	public String toString(){
		return name + ", " + type + ", " + color + co.toString();
	}
	
	public boolean hasMoved() {
		// TODO Auto-generated method stub
		return moved;
	}
	
	@Override
	public void setSelected(boolean b) {
		selected = b;
	}
	
	@Override
	public boolean isSelected() {
		return selected;
	}
	
	public Coordinate getLastCoordinate(){
		return lastCo;
	}
	


}